﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App.Domain.Contracts;
using App.Domain.Models;

namespace App.Domain
{
    public class UserServices : IUserServices
    {
        //private readonly IUserRepository userRepository;

        public UserServices()
        {
            //if (userRepository == null) throw new ArgumentNullException("userRepository");
            //this.userRepository = userRepository;
        }

        public void UpdateUser(User updatedUser)
        {
            throw new NotImplementedException();
        }

        public User GetOrCreateUser(string userId, string userPwd)
        {
            User user = null;
            try
            {
                user = this.GetUserByClaimedIdentifier(userId);
                if (user.UserPwd.Equals("111"))
                {
                    return user;
                }
            }
            catch (Exception ex)
            {
                user = null;
            }

            if (user == null)
            {
                user = new User()
                {
                    AuthorizationId = userId,
                    DisplayName = "",
                };

                user = this.CreateUser(user);
            }

            return user;
        }

        public User CreateUser(User newUser)
        {
            if (newUser == null) throw new ArgumentNullException("newUser");
            try
            {
                User userToAdd = ToDataModelUser(newUser);
                //this.userRepository.Create(userToAdd);
                return ToServiceUser(userToAdd);
            }
            catch (InvalidOperationException ex)
            {
                throw ex;
            }
        }

        internal static User ToDataModelUser(User userToConvert)
        {
            if (userToConvert == null)
            {
                return null;
            }

            User modelUser = new User()
            {
                UserId = userToConvert.UserId,
                AuthorizationId = userToConvert.AuthorizationId,
                DisplayName = userToConvert.DisplayName,
                Country = userToConvert.Country,
                PostalCode = userToConvert.PostalCode,
                HasRegistered = userToConvert.HasRegistered,
            };
            return modelUser;
        }

        internal static User ToServiceUser(User dataUser)
        {
            if (dataUser == null)
            {
                return null;
            }

            User user = new User()
            {
                UserId = dataUser.UserId,
                AuthorizationId = dataUser.AuthorizationId,
                DisplayName = dataUser.DisplayName,
                Country = dataUser.Country,
                PostalCode = dataUser.PostalCode,
                HasRegistered = dataUser.HasRegistered,
            };
            return user;
        }

        public User GetUserByClaimedIdentifier(string userName)
        {
            return new User() { 
            
                UserId=1111,
                AuthorizationId="afsdf",
                DisplayName="bbb",
                UserPwd="111",

            };
        }
    }
}
